package baiyuanwei.example.calendar.ui;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

import java.util.ArrayList;

/**
 * Created by baiyuanwei on 16/5/3.
 * <p/>
 * 注意:
 * 1、canvas.drawText()方法中定位字的位置时，默认是字的左小角，可以利用paint.setTextAlign(Paint.Align.CENTER)来设置横坐标以字的中心为标准，纵坐标仍然是字的底部为标准
 */
public class WeekView extends View {

    //周六 周天的颜色
    private final int DEFAULT_SAT_SUN_COLOR = Color.RED;

    //周一 ~ 周五的颜色
    private final int DEFAULT_DAYS_COLOR = Color.BLACK;

    //默认字体的大小(还要乘以密度)
    private final int DEFAULT_TEXT_SIZE = 12;

    //画笔的宽度
    private final int DEFAULT_PAINT_WIDTH = 1;

    //默认宽高(还要乘以密度)
    private final int DEFAULT_WIDTH = 300;
    private final int DEFAULT_HEIGHT = 40;

    //屏幕的密度
    private float density;

    //此view的宽高
    private int width;
    private int height;

    //默认字体大小
    private int paintTextSize;

    private Paint paint;

    //默认的一周
    private String[] weeks = new String[]{"周日", "周一", "周二", "周三", "周四", "周五", "周六"};
    private String sat = "周六";
    private String sun = "周日";

    public WeekView(Context context) {
        this(context, null);
    }

    public WeekView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public WeekView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);


        init();
    }

    private void init() {

        //获取屏幕密度
        density = getContext().getResources().getDisplayMetrics().density;

        //默认字体大小
        paintTextSize = (int) (DEFAULT_TEXT_SIZE * density);


        //画笔的默认配置
        paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setStrokeWidth(DEFAULT_PAINT_WIDTH);
        paint.setTextSize(paintTextSize);
        paint.setTextAlign(Paint.Align.CENTER);

    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

        int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
        int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec);

        int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
        int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec);

        if (widthSpecMode == MeasureSpec.AT_MOST && heightSpecMode == MeasureSpec.AT_MOST) {
            setMeasuredDimension((int) (DEFAULT_WIDTH * density), (int) (DEFAULT_HEIGHT * density));
        } else if (widthSpecMode == MeasureSpec.AT_MOST) {
            setMeasuredDimension((int) (DEFAULT_WIDTH * density), heightSpecSize);
        } else if (heightSpecMode == MeasureSpec.AT_MOST) {
            setMeasuredDimension(widthSpecSize, (int) (DEFAULT_HEIGHT * density));
        } else {
            setMeasuredDimension(widthSpecSize, heightSpecSize);
        }
    }

    @Override
    protected void onDraw(Canvas canvas) {
        drawWeek(canvas);


    }

    /**
     * 画星期的标题
     *
     * @param canvas
     */
    private void drawWeek(Canvas canvas) {
        canvas.save();

        int weekCount = weeks.length;

        int startX = (width - 7 * paintTextSize) / (weekCount * 2) + paintTextSize / 2;
        int y = height / 2 + paintTextSize / 2;
        int x = startX;


        for (int i = 0; i < weekCount; i++) {
            if (weeks[i].equals(sat) || weeks[i].equals(sun)) {
                paint.setColor(DEFAULT_SAT_SUN_COLOR);
            } else {
                paint.setColor(DEFAULT_DAYS_COLOR);
            }
            canvas.drawText(weeks[i], x, y, paint);
            x = x + 2 * startX;
        }

        canvas.restore();
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {

        width = getWidth();
        height = getHeight();

        super.onLayout(changed, left, top, right, bottom);
    }
}
